﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Grinder.Properties;
using Grinder.Renamer;

namespace Grinder.Forms
{
    public sealed partial class TemplateEditorForm : Form
    {
        public string Template
        {
            get { return textTemplate.Text; }
            set { textTemplate.Text = value; }
        }

        public IEnumerable<string> Templates
        {
            get
            {
                var templates = new List<string>();

                foreach (ListViewItem item in listFavouriteTemplates.Items)
                    templates.Add(item.Text);

                return templates;
            }
            set
            {
                foreach (var item in value)
                    listFavouriteTemplates.Items.Add(item);
                columnFavTemplate.Width = -1;
            }
        }

        public TemplateEditorForm()
        {
            InitializeComponent();
        }

        public TemplateEditorForm(IEnumerable<IRenamePlugin> plugins)
            : this()
        {
            foreach (var plugin in plugins)
                comboPlugins.Items.Add(plugin);

            if (comboPlugins.Items.Count > 0)
                comboPlugins.SelectedIndex = 0;

            var imageList = new ImageList { ColorDepth = ColorDepth.Depth32Bit };

            imageList.Images.Add(Resources.plugin_disabled);

            listTokens.SmallImageList = imageList;
        }

        public DialogResult ShowDialog(string template)
        {
            textTemplate.Text = template;
            return ShowDialog();
        }

        private void comboPlugins_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();

            if (e.Index < 0)
                return;

            var rectangle = new Rectangle(e.Bounds.X + 20, e.Bounds.Y + 1, e.Bounds.Width, e.Bounds.Height);
            var iconRect = new Rectangle(e.Bounds.X + 2, e.Bounds.Y, 15, 15);
            var plugin = (IRenamePlugin)comboPlugins.Items[e.Index];

            var col = Color.White;

            if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
                col = Color.AliceBlue;

            e.Graphics.FillRectangle(new SolidBrush(col), e.Bounds);

            var myFont = comboPlugins.Font;

            e.Graphics.DrawString(comboPlugins.Items[e.Index].ToString(), myFont, Brushes.Black, rectangle);

            if (plugin.Icon != null)
                e.Graphics.DrawImage(plugin.Icon, iconRect);

            e.DrawFocusRectangle();
        }

        private void comboPlugins_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight = 25;
        }

        private void comboPlugins_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboPlugins.SelectedItem == null) return;

            var plugin = (IRenamePlugin)comboPlugins.SelectedItem;

            listTokens.Items.Clear();

            foreach (var token in plugin.Tokens)
            {
                var item = listTokens.Items.Add("", 0);
                item.SubItems.Add(string.Format("{0}{1}{2}", RenamerConstants.LeftDelimiter, token.Key, RenamerConstants.RightDelimiter));
                item.SubItems.Add(token.Value);
            }
        }

        private void listTokens_DoubleClick(object sender, EventArgs e)
        {
            if (listTokens.SelectedItems.Count <= 0) return;

            string token = listTokens.SelectedItems[0].SubItems[1].Text;

            if (textTemplate.SelectionStart > textTemplate.Text.Length)
                textTemplate.SelectionStart = textTemplate.Text.Length;

            textTemplate.Text = textTemplate.Text.Insert(textTemplate.SelectionStart, token);

            textTemplate.SelectionStart += token.Length;
        }

        private void btnAddTemplate_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textTemplate.Text))
            {
                listFavouriteTemplates.Items.Add(textTemplate.Text);
                columnFavTemplate.Width = -1;
            }
        }

        private void btnRemoveTemplate_Click(object sender, EventArgs e)
        {
            listFavouriteTemplates.RemoveSelected();
        }

        private void listSavedTemplates_DoubleClick(object sender, EventArgs e)
        {
            if (listFavouriteTemplates.SelectedItems.Count > 0)
                textTemplate.Text = listFavouriteTemplates.SelectedItems[0].Text;
        }
    }
}
